package me.qingy.programmatic;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
 * @author qingy
 * @since 2021/8/18
 */
public class ReverseString {
    /**
     * for 版双指针
     */
    public char[] reverseString1(char[] s) {
        int j = s.length - 1;
        for (int i = 0; i < j; i++) {
            char tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;
            j--;
        }
        return s;
    }

    /**
     * while 版双指针
     */
    public char[] reverseString2(char[] s) {
        int n = s.length;
        int i = 0;
        int j = n - 1;
        while (i < j) {
            char tmp = s[i];
            s[i] = s[j];
            s[j] = tmp;

            i++;
            j--;
        }
        return s;
    }

    public char[] reverseString3(char[] s) {
        int n = s.length;
        for (int i = 0; i < n / 2; i++) {
            char tmp = s[i];
            s[i] = s[n - 1 - i];
            s[n - 1 - i] = tmp;
        }
        return s;
    }


    @Test
    public void testReverseString() {
        Assertions.assertArrayEquals(new char[]{'o', 'l', 'l', 'e', 'h'}, reverseString3("hello".toCharArray()));
    }
}
